<!--
 * @Author: MarsLay
 * @Date: 2021-12-27 10:54:10
 * @LastEditTime: 2023-12-29 17:50:04
 * @LastEditors: MarsLay 331412466@qq.com
 * @Description: 计数器
 * @FilePath: components\inputNumber\inputNumber.vue
-->
<template>
  <view class="inputNumber" :class="{ 'disable': disabled }">
    <image v-if="icon.length > 0" @click="minus" mode="aspectFill"
      :style="{ width: iconSize + 'px', height: iconSize + 'px' }" :src="icon[0]"></image>
    <view v-else class="minus handler" :class="{ 'disable': dnum <= min }" @click="minus">-</view>
    <input :class="{ 'noborder': icon.length > 0 }" type="number" :placeholder="placeholder" v-model="dnum" @blur="blur">
    <image v-if="icon.length > 0" @click="plus" mode="aspectFill"
      :style="{ width: iconSize + 'px', height: iconSize + 'px' }" :src="icon[1]"></image>
    <view v-else class="plus handler" :class="{ 'disable': dnum >= max }" @click="plus">+</view>
  </view>
</template>

<script>
export default {
  name: "inputNumber",
  components: {},
  props: {
    mark: {
      type: String,
      default: "",
    },
    num: {
      type: [String, Number],
      default: 0,
    },
    min: {
      type: [String, Number],
    },
    max: {
      type: [String, Number],
      default: 9999999
    },
    step: {
      type: [String, Number],
      default: 0,
    },
    placeholder: {
      type: String,
    },
    icon: {
      type: Array,
      default: () => [],
    },
    iconSize: {
      type: Number,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      dnum: this.num,
    };
  },
  computed: {},
  watch: {
    dnum(v) {
      if (v <= this.min) {
        this.dnum = this.min;
      }
      if (v >= this.max) {
        this.dnum = this.max;
      }
      this.$emit("update:num", this.dnum);
      this.$emit("change", { value: v, key: this.mark });
    },
  },
  onLoad(d) { },
  onShow() { },
  methods: {
    minus() {
      if (this.step > 0) {
        this.dnum -= this.step;
      } else {
        this.dnum--;
      }
    },
    plus() {
      if (this.step > 0) {
        this.dnum += this.step;
      } else {
        this.dnum++;
      }
    },
    blur(v) {
      if (v.detail.value <= this.min) {
        this.dnum = this.min;
      }
      if (v.detail.value >= this.max) {
        this.dnum = this.max;
      }

      this.dnum = parseInt(v.detail.value);
    },
  },
};
</script>
<style lang='less' scoped>
.inputNumber {
  .flex(row, flex-start);
}

.handler {
  width: 80upx;
  height: 80upx;
  line-height: 74upx;
  text-align: center;
  font-size: 26upx;
  color: #606266;
  background: #f5f7fa;
  border: 1upx solid #dcdfe6;
}

.disable {
  pointer-events: none;
  color: #c0c4cc;
  cursor: not-allowed;
}

.inputNumber.disable {
  .handler {
    border-color: #e4e7ed;
    color: #e4e7ed;
  }

  uni-input {
    background-color: #f5f7fa;
    border-color: #e4e7ed;
    color: #c0c4cc;
    cursor: not-allowed;
  }
}

.noborder {
  border: 0;
}

uni-input,
input {
  width: 160upx;
  height: 80upx;
  line-height: 74upx;
  text-align: center;
  color: #606266;
  font-size: 26upx;
  border-top: 1upx solid #dcdfe6;
  border-bottom: 1upx solid #dcdfe6;
  box-sizing: border-box;
}
</style>